﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERPSystemApplication
{
    public partial class FrmSupplierManagement : Form
    {
        string _buttonstatus = "";

        public FrmSupplierManagement()
        {
            InitializeComponent();
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            string CompanyName = txtCompanyName.Text.Trim();
            string Contacts = txtContacts.Text.Trim();
            string ContactsPhone = txtContactsPhone.Text.Trim();

            BindData();

            _buttonstatus = "";
            SetButtonStatus();
        }

        private void BindData()
        {
            List<MODEL.Supplier> SupplierList = SysController.SupplierController.GetListByWhere(s=>s.IsDel==false);
            dgvData.DataSource = SupplierList;
        }

        private void dgvData_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == -1 || e.RowIndex == -1)
            {
                return;
            }

            SetTextWithRowValue();
        }

        private void SetTextWithRowValue()
        {
            txtCompanyAreaEdit.Text = (dgvData.CurrentRow.Cells["colCompanyArea"].Value??"").ToString();
            txtCompanyNameEdit.Text = (dgvData.CurrentRow.Cells["colCompanyName"].Value??"").ToString();
            txtCorporateBodyEdit.Text = (dgvData.CurrentRow.Cells["colCorporateBody"].Value??"").ToString();
            txtTelEdit.Text = (dgvData.CurrentRow.Cells["colTel"].Value??"").ToString();
            txtEmailEdit.Text = (dgvData.CurrentRow.Cells["colEmail"].Value??"").ToString();
            txtContactsEdit.Text = (dgvData.CurrentRow.Cells["colContacts"].Value??"").ToString();
            txtContactsPhoneEdit.Text =( dgvData.CurrentRow.Cells["colContactsPhone"].Value??"").ToString();
            txtContactsMsgEdit.Text = (dgvData.CurrentRow.Cells["colContactsMsg"].Value??"").ToString();
            txtSupplierIDEdit.Text = (dgvData.CurrentRow.Cells["colSupplierID"].Value ?? "").ToString();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定要保存吗？", "ERPSystem", MessageBoxButtons.YesNo) == DialogResult.No)
                return;

            if (string.IsNullOrWhiteSpace(this.txtCompanyNameEdit.Text))
            {
                MessageBox.Show("请填写公司名称!");
                return;
            }
            if (string.IsNullOrWhiteSpace(this.txtCorporateBodyEdit.Text))
            {
                MessageBox.Show("请填写公司法人名字!");
                return;
            }
            if (string.IsNullOrWhiteSpace(this.txtTelEdit.Text))
            {
                MessageBox.Show("请填写公司法人联系电话!");
                return;
            }

            try
            {
                MODEL.Supplier model = new MODEL.Supplier();
                model.CompanyName = txtCompanyNameEdit.Text;
                model.CompanyArea = txtCompanyAreaEdit.Text;
                model.Contacts = txtContactsEdit.Text;
                model.ContactsMsg = txtContactsMsgEdit.Text;
                model.ContactsPhone = txtContactsPhoneEdit.Text;
                model.CorporateBody = txtCorporateBodyEdit.Text;
                model.CreateTime = DateTime.Now;
                model.CreateUser = "System";
                model.Email = txtEmailEdit.Text;
                model.IsDel = false;
                model.SupplierLevel = 2;
                model.Tel = txtTelEdit.Text;
                model.SupplierID = string.IsNullOrWhiteSpace(txtSupplierIDEdit.Text) ? 0 : Convert.ToInt32(txtSupplierIDEdit.Text);



                List<MODEL.Supplier> SupplierList = new List<MODEL.Supplier>();
                SupplierList.Add(model);
                int count = SysController.SupplierController.ModifyIfNullAddSupplier(SupplierList);
                if (count > 0)
                {
                    MessageBox.Show("保存成功!");
                }
                else
                {
                    MessageBox.Show("保存失败!");
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show("保存失败：" + ex.Message);
            }

            BindData();
            SetButtonStatus();
        }

        private void SetButtonStatus()
        {
            switch (_buttonstatus)
            {
                case "":
                    btnAdd.Enabled = true;
                    btnSave.Enabled = false;
                    btnEdit.Enabled = true;
                    txtCompanyAreaEdit.ReadOnly = true;
                    txtCompanyNameEdit.ReadOnly = true;
                    txtContactsEdit.ReadOnly = true;
                    txtContactsMsgEdit.ReadOnly = true;
                    txtContactsPhoneEdit.ReadOnly = true;
                    txtCorporateBodyEdit.ReadOnly = true;
                    txtEmailEdit.ReadOnly = true;
                    txtTelEdit.ReadOnly = true;
                    break;
                case "add":
                    btnAdd.Enabled = false;
                    btnSave.Enabled = true;
                    btnEdit.Enabled = false;
                    txtCompanyAreaEdit.ReadOnly = false;
                    txtCompanyNameEdit.ReadOnly = false;
                    txtContactsEdit.ReadOnly = false;
                    txtContactsMsgEdit.ReadOnly = false;
                    txtContactsPhoneEdit.ReadOnly = false;
                    txtCorporateBodyEdit.ReadOnly = false;
                    txtEmailEdit.ReadOnly = false;
                    txtTelEdit.ReadOnly = false;
                    break;
                case "edit":
                    btnAdd.Enabled = false;
                    btnSave.Enabled = true;
                    btnEdit.Enabled = false;
                    txtCompanyAreaEdit.ReadOnly = false;
                    txtCompanyNameEdit.ReadOnly = false;
                    txtContactsEdit.ReadOnly = false;
                    txtContactsMsgEdit.ReadOnly = false;
                    txtContactsPhoneEdit.ReadOnly = false;
                    txtCorporateBodyEdit.ReadOnly = false;
                    txtEmailEdit.ReadOnly = false;
                    txtTelEdit.ReadOnly = false;
                    break;
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            _buttonstatus = "edit";
            SetButtonStatus();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            txtCompanyAreaEdit.Text = "";
            txtCompanyNameEdit.Text = "";
            txtContactsEdit.Text = "";
            txtContactsMsgEdit.Text = "";
            txtContactsPhoneEdit.Text = "";
            txtCorporateBodyEdit.Text = "";
            txtEmailEdit.Text = "";
            txtTelEdit.Text = "";
            txtSupplierIDEdit.Text = "";

            _buttonstatus = "add";
            SetButtonStatus();
        }

        private void VendorManagement_Load(object sender, EventArgs e)
        {
            //BindData();
            SetButtonStatus();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (dgvData.CurrentRow != null)
            {
                SetTextWithRowValue();
            }

            _buttonstatus = "";
            SetButtonStatus();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(txtSupplierIDEdit.Text))
                {
                    MessageBox.Show("当前无数据可删除！");
                    return;
                }

                MODEL.Supplier model = new MODEL.Supplier();
                model.SupplierID = Convert.ToInt32(txtSupplierIDEdit.Text.Trim());
                List<MODEL.Supplier> SupplierList=new List<MODEL.Supplier>();
                SupplierList.Add(model);

                int count = SysController.SupplierController.DelSupplier(SupplierList);
                string msg = "";
                if (count > 0)
                {
                    msg = "删除成功!";
                }
                else
                {
                    msg = "删除失败！";
                }

                MessageBox.Show(msg);
            }
            catch (Exception ex)
            {
                MessageBox.Show("删除失败：" + ex.Message);
            }

            _buttonstatus = "";
            SetButtonStatus();
            BindData();
        }
    }
}
